{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = pd.read_csv('D:/Python2025/000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day',inplace=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.87</td>\n",
       "      <td>637.7200</td>\n",
       "      <td>647.7100</td>\n",
       "      <td>630.5300</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.88</td>\n",
       "      <td>641.9000</td>\n",
       "      <td>655.5100</td>\n",
       "      <td>638.8600</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>639.8800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.81</td>\n",
       "      <td>655.3800</td>\n",
       "      <td>657.5200</td>\n",
       "      <td>645.8100</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>653.8100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.89</td>\n",
       "      <td>642.7500</td>\n",
       "      <td>643.8900</td>\n",
       "      <td>636.3300</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>646.8900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.76</td>\n",
       "      <td>637.5200</td>\n",
       "      <td>637.5500</td>\n",
       "      <td>625.0400</td>\n",
       "      <td>626.0000</td>\n",
       "      <td>640.7600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2901.9518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2886.7416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2890.8973</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2891.8453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2879.2996</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7182 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close   PreClose\n",
       "Day                                                                         \n",
       "1995-01-03     647.87   637.7200   647.7100   630.5300   639.8800        NaN\n",
       "1995-01-04     639.88   641.9000   655.5100   638.8600   653.8100   639.8800\n",
       "1995-01-05     653.81   655.3800   657.5200   645.8100   646.8900   653.8100\n",
       "1995-01-06     646.89   642.7500   643.8900   636.3300   640.7600   646.8900\n",
       "1995-01-09     640.76   637.5200   637.5500   625.0400   626.0000   640.7600\n",
       "...               ...        ...        ...        ...        ...        ...\n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416  2901.9518\n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973  2886.7416\n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453  2890.8973\n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996  2891.8453\n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493  2879.2996\n",
       "\n",
       "[7182 rows x 6 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['1995':'2024-07'].copy() \n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['PreClose'] = data_new['Close'].shift(1)\n",
    "data_new = pd.DataFrame(data_new)\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Close  PreClose\n",
      "Day                         \n",
      "1995-01-03  639.88       NaN\n",
      "1995-01-04  653.81    639.88\n",
      "1995-01-05  646.89    653.81\n",
      "1995-01-06  640.76    646.89\n",
      "1995-01-09  626.00    640.76\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.87</td>\n",
       "      <td>637.7200</td>\n",
       "      <td>647.7100</td>\n",
       "      <td>630.5300</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.88</td>\n",
       "      <td>641.9000</td>\n",
       "      <td>655.5100</td>\n",
       "      <td>638.8600</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.81</td>\n",
       "      <td>655.3800</td>\n",
       "      <td>657.5200</td>\n",
       "      <td>645.8100</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.89</td>\n",
       "      <td>642.7500</td>\n",
       "      <td>643.8900</td>\n",
       "      <td>636.3300</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.76</td>\n",
       "      <td>637.5200</td>\n",
       "      <td>637.5500</td>\n",
       "      <td>625.0400</td>\n",
       "      <td>626.0000</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2901.9518</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>-0.005241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0.001440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0.000328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>-0.004338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0.020647</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7182 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close   PreClose  \\\n",
       "Day                                                                            \n",
       "1995-01-03     647.87   637.7200   647.7100   630.5300   639.8800        NaN   \n",
       "1995-01-04     639.88   641.9000   655.5100   638.8600   653.8100   639.8800   \n",
       "1995-01-05     653.81   655.3800   657.5200   645.8100   646.8900   653.8100   \n",
       "1995-01-06     646.89   642.7500   643.8900   636.3300   640.7600   646.8900   \n",
       "1995-01-09     640.76   637.5200   637.5500   625.0400   626.0000   640.7600   \n",
       "...               ...        ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416  2901.9518   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973  2886.7416   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453  2890.8973   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996  2891.8453   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493  2879.2996   \n",
       "\n",
       "              Return  Return_2  \n",
       "Day                             \n",
       "1995-01-03       NaN       NaN  \n",
       "1995-01-04  0.021770  0.021770  \n",
       "1995-01-05 -0.010584 -0.010584  \n",
       "1995-01-06 -0.009476 -0.009476  \n",
       "1995-01-09 -0.023035 -0.023035  \n",
       "...              ...       ...  \n",
       "2024-07-25 -0.005241 -0.005241  \n",
       "2024-07-26  0.001440  0.001440  \n",
       "2024-07-29  0.000328  0.000328  \n",
       "2024-07-30 -0.004338 -0.004338  \n",
       "2024-07-31  0.020647  0.020647  \n",
       "\n",
       "[7182 rows x 8 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return'] = (data_new['Close'] / data_new['PreClose']) - 1\n",
    "print(data_new[['Close', 'PreClose']].head())\n",
    "data_new['Return_2'] = data_new['Return']\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-05-18</th>\n",
       "      <td>582.89</td>\n",
       "      <td>741.81</td>\n",
       "      <td>770.82</td>\n",
       "      <td>682.01</td>\n",
       "      <td>763.51</td>\n",
       "      <td>582.89</td>\n",
       "      <td>0.309870</td>\n",
       "      <td>0.309870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-19</th>\n",
       "      <td>763.51</td>\n",
       "      <td>768.82</td>\n",
       "      <td>855.81</td>\n",
       "      <td>761.38</td>\n",
       "      <td>855.81</td>\n",
       "      <td>763.51</td>\n",
       "      <td>0.120889</td>\n",
       "      <td>0.120889</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Preclose    Open  Highest  Lowest   Close  PreClose    Return  \\\n",
       "Day                                                                        \n",
       "1995-05-18   582.89  741.81   770.82  682.01  763.51    582.89  0.309870   \n",
       "1995-05-19   763.51  768.82   855.81  761.38  855.81    763.51  0.120889   \n",
       "\n",
       "            Return_2  \n",
       "Day                   \n",
       "1995-05-18  0.309870  \n",
       "1995-05-19  0.120889  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new[data_new['Return'] > 0.1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "      <th>diff</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.87</td>\n",
       "      <td>637.7200</td>\n",
       "      <td>647.7100</td>\n",
       "      <td>630.5300</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.88</td>\n",
       "      <td>641.9000</td>\n",
       "      <td>655.5100</td>\n",
       "      <td>638.8600</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.81</td>\n",
       "      <td>655.3800</td>\n",
       "      <td>657.5200</td>\n",
       "      <td>645.8100</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.89</td>\n",
       "      <td>642.7500</td>\n",
       "      <td>643.8900</td>\n",
       "      <td>636.3300</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.76</td>\n",
       "      <td>637.5200</td>\n",
       "      <td>637.5500</td>\n",
       "      <td>625.0400</td>\n",
       "      <td>626.0000</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2901.9518</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7182 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close   PreClose  \\\n",
       "Day                                                                            \n",
       "1995-01-03     647.87   637.7200   647.7100   630.5300   639.8800        NaN   \n",
       "1995-01-04     639.88   641.9000   655.5100   638.8600   653.8100   639.8800   \n",
       "1995-01-05     653.81   655.3800   657.5200   645.8100   646.8900   653.8100   \n",
       "1995-01-06     646.89   642.7500   643.8900   636.3300   640.7600   646.8900   \n",
       "1995-01-09     640.76   637.5200   637.5500   625.0400   626.0000   640.7600   \n",
       "...               ...        ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416  2901.9518   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973  2886.7416   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453  2890.8973   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996  2891.8453   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493  2879.2996   \n",
       "\n",
       "              Return  Return_2  diff  \n",
       "Day                                   \n",
       "1995-01-03       NaN       NaN   NaN  \n",
       "1995-01-04  0.021770  0.021770   0.0  \n",
       "1995-01-05 -0.010584 -0.010584   0.0  \n",
       "1995-01-06 -0.009476 -0.009476   0.0  \n",
       "1995-01-09 -0.023035 -0.023035   0.0  \n",
       "...              ...       ...   ...  \n",
       "2024-07-25 -0.005241 -0.005241   0.0  \n",
       "2024-07-26  0.001440  0.001440   0.0  \n",
       "2024-07-29  0.000328  0.000328   0.0  \n",
       "2024-07-30 -0.004338 -0.004338   0.0  \n",
       "2024-07-31  0.020647  0.020647   0.0  \n",
       "\n",
       "[7182 rows x 9 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['diff'] = data_new['Return'] - data_new['Return_2']\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两种方法的差异： 0.0\n"
     ]
    }
   ],
   "source": [
    "print('两种方法的差异：', data_new['diff'].sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算上证综指月度、季度、年度的收益率\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "算法一"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.data_new日度数据里面选择月的最后一天close 2.把这个close更改一下变成上个月的最后一天 3.计算月度收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.5900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.2600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.9200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.9300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.5100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close\n",
       "Day                  \n",
       "1995-01-31   562.5900\n",
       "1995-02-28   549.2600\n",
       "1995-03-31   646.9200\n",
       "1995-04-30   579.9300\n",
       "1995-05-31   700.5100\n",
       "...               ...\n",
       "2024-03-31  3041.1669\n",
       "2024-04-30  3104.8245\n",
       "2024-05-31  3086.8134\n",
       "2024-06-30  2967.4028\n",
       "2024-07-31  2938.7493\n",
       "\n",
       "[355 rows x 1 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data = data_new.resample('ME')['Close'].last().to_frame()\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.5900</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.2600</td>\n",
       "      <td>562.5900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.9200</td>\n",
       "      <td>549.2600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.9300</td>\n",
       "      <td>646.9200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.5100</td>\n",
       "      <td>579.9300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3015.1712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3041.1669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>3104.8245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>3086.8134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2967.4028</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   PreClose\n",
       "Day                             \n",
       "1995-01-31   562.5900        NaN\n",
       "1995-02-28   549.2600   562.5900\n",
       "1995-03-31   646.9200   549.2600\n",
       "1995-04-30   579.9300   646.9200\n",
       "1995-05-31   700.5100   579.9300\n",
       "...               ...        ...\n",
       "2024-03-31  3041.1669  3015.1712\n",
       "2024-04-30  3104.8245  3041.1669\n",
       "2024-05-31  3086.8134  3104.8245\n",
       "2024-06-30  2967.4028  3086.8134\n",
       "2024-07-31  2938.7493  2967.4028\n",
       "\n",
       "[355 rows x 2 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['PreClose'] = Month_data['Close'].shift(1)\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.5900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.2600</td>\n",
       "      <td>562.5900</td>\n",
       "      <td>-0.023694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.9200</td>\n",
       "      <td>549.2600</td>\n",
       "      <td>0.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.9300</td>\n",
       "      <td>646.9200</td>\n",
       "      <td>-0.103552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.5100</td>\n",
       "      <td>579.9300</td>\n",
       "      <td>0.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3015.1712</td>\n",
       "      <td>0.008622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3041.1669</td>\n",
       "      <td>0.020932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>3104.8245</td>\n",
       "      <td>-0.005801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>3086.8134</td>\n",
       "      <td>-0.038684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2967.4028</td>\n",
       "      <td>-0.009656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   PreClose    Return\n",
       "Day                                       \n",
       "1995-01-31   562.5900        NaN       NaN\n",
       "1995-02-28   549.2600   562.5900 -0.023694\n",
       "1995-03-31   646.9200   549.2600  0.177803\n",
       "1995-04-30   579.9300   646.9200 -0.103552\n",
       "1995-05-31   700.5100   579.9300  0.207922\n",
       "...               ...        ...       ...\n",
       "2024-03-31  3041.1669  3015.1712  0.008622\n",
       "2024-04-30  3104.8245  3041.1669  0.020932\n",
       "2024-05-31  3086.8134  3104.8245 -0.005801\n",
       "2024-06-30  2967.4028  3086.8134 -0.038684\n",
       "2024-07-31  2938.7493  2967.4028 -0.009656\n",
       "\n",
       "[355 rows x 3 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Return'] = (Month_data['Close'] / Month_data['PreClose']) - 1\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Close  PreClose    Return\n",
      "Day                                    \n",
      "2008-10-31  1728.79   2293.78 -0.246314\n"
     ]
    }
   ],
   "source": [
    "result = Month_data[Month_data['Return'] == Month_data['Return'].min()]\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1999-06-30</th>\n",
       "      <td>1689.43</td>\n",
       "      <td>1279.33</td>\n",
       "      <td>0.320558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  PreClose    Return\n",
       "Day                                    \n",
       "1999-06-30  1689.43   1279.33  0.320558"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data[Month_data['Return'] == Month_data['Return'].max()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "算法二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Return_plus1</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>0.879212</td>\n",
       "      <td>-0.120788</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>0.976306</td>\n",
       "      <td>-0.023694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>1.177803</td>\n",
       "      <td>0.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>0.896448</td>\n",
       "      <td>-0.103552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>1.207922</td>\n",
       "      <td>0.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>1.008622</td>\n",
       "      <td>0.008622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>1.020932</td>\n",
       "      <td>0.020932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>0.994199</td>\n",
       "      <td>-0.005801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>0.961316</td>\n",
       "      <td>-0.038684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>0.990344</td>\n",
       "      <td>-0.009656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Return_plus1    Return\n",
       "Day                               \n",
       "1995-01-31      0.879212 -0.120788\n",
       "1995-02-28      0.976306 -0.023694\n",
       "1995-03-31      1.177803  0.177803\n",
       "1995-04-30      0.896448 -0.103552\n",
       "1995-05-31      1.207922  0.207922\n",
       "...                  ...       ...\n",
       "2024-03-31      1.008622  0.008622\n",
       "2024-04-30      1.020932  0.020932\n",
       "2024-05-31      0.994199 -0.005801\n",
       "2024-06-30      0.961316 -0.038684\n",
       "2024-07-31      0.990344 -0.009656\n",
       "\n",
       "[355 rows x 2 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return_plus1'] = data_new['Return'] + 1\n",
    "Month_data2 = data_new.resample('ME')['Return_plus1'].prod().to_frame()\n",
    "Month_data2['Return'] = Month_data2['Return_plus1'] - 1\n",
    "Month_data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "年度收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>917.0200</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>1194.1000</td>\n",
       "      <td>917.0200</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>1146.7000</td>\n",
       "      <td>1194.1000</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>1366.5800</td>\n",
       "      <td>1146.7000</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.4800</td>\n",
       "      <td>1366.5800</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.9700</td>\n",
       "      <td>2073.4800</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1645.9700</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>2974.9348</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>-0.012163</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   PreClose    Return\n",
       "Day                                       \n",
       "1995-12-31   555.2900        NaN       NaN\n",
       "1996-12-31   917.0200   555.2900  0.651425\n",
       "1997-12-31  1194.1000   917.0200  0.302153\n",
       "1998-12-31  1146.7000  1194.1000 -0.039695\n",
       "1999-12-31  1366.5800  1146.7000  0.191750\n",
       "2000-12-31  2073.4800  1366.5800  0.517277\n",
       "2001-12-31  1645.9700  2073.4800 -0.206180\n",
       "2002-12-31  1357.6500  1645.9700 -0.175167\n",
       "2003-12-31  1497.0400  1357.6500  0.102670\n",
       "2004-12-31  1266.5000  1497.0400 -0.153997\n",
       "2005-12-31  1161.0600  1266.5000 -0.083253\n",
       "2006-12-31  2675.4700  1161.0600  1.304334\n",
       "2007-12-31  5261.5600  2675.4700  0.966593\n",
       "2008-12-31  1820.8100  5261.5600 -0.653941\n",
       "2009-12-31  3277.1400  1820.8100  0.799825\n",
       "2010-12-31  2808.0800  3277.1400 -0.143131\n",
       "2011-12-31  2199.4200  2808.0800 -0.216753\n",
       "2012-12-31  2269.1300  2199.4200  0.031695\n",
       "2013-12-31  2115.9800  2269.1300 -0.067493\n",
       "2014-12-31  3234.6800  2115.9800  0.528691\n",
       "2015-12-31  3539.1800  3234.6800  0.094136\n",
       "2016-12-31  3103.6400  3539.1800 -0.123062\n",
       "2017-12-31  3307.1700  3103.6400  0.065578\n",
       "2018-12-31  2493.9000  3307.1700 -0.245911\n",
       "2019-12-31  3050.1200  2493.9000  0.223032\n",
       "2020-12-31  3473.0700  3050.1200  0.138667\n",
       "2021-12-31  3639.7800  3473.0700  0.048001\n",
       "2022-12-31  3089.2579  3639.7800 -0.151251\n",
       "2023-12-31  2974.9348  3089.2579 -0.037007\n",
       "2024-12-31  2938.7493  2974.9348 -0.012163"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Year_data = data_new.resample('YE')['Close'].last().to_frame()\n",
    "Year_data['PreClose'] = Year_data['Close'].shift(1)\n",
    "Year_data['Return'] = (Year_data['Close'] / Year_data['PreClose']) - 1\n",
    "Year_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>PreClose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.81</td>\n",
       "      <td>5261.56</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  PreClose    Return\n",
       "Day                                    \n",
       "2008-12-31  1820.81   5261.56 -0.653941"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Year_data[Year_data['Return'] == Year_data['Return'].min()]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
